package com.avic.rong.wedgit.customView;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.avic.rong.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/15 20:22
 * E-Mail Address：16601203965@163.com
 */
public class SearchWordItermAdapter extends RecyclerView.Adapter<SearchWordItermAdapter.SearchItemViewHolder> {

    private Context context;
    private List<ItemSearchBean.WordBean> dataList = new ArrayList<>();


    public void setDataList(List<ItemSearchBean.WordBean> dataList) {
        this.dataList = dataList;
    }

    public SearchWordItermAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public SearchItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_word_search, viewGroup, false);
        return new SearchItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final SearchItemViewHolder holder, final int position) {

        ItemSearchBean.WordBean searchBean = dataList.get(position);

        holder.tv_name.setText(searchBean.getName());
        holder.edit_value.setText(searchBean.getValue());
        holder.edit_value.setHint("请输入" + searchBean.getName());
        holder.edit_value.addTextChangedListener(new MyTextWatcher(searchBean));

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    class MyTextWatcher implements TextWatcher {

        ItemSearchBean.WordBean searchBean;

        public MyTextWatcher(ItemSearchBean.WordBean searchBean) {
            this.searchBean = searchBean;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String currentString = s.toString();
            if (!TextUtils.isEmpty(currentString)) {
                currentString = currentString.trim();
            }
            searchBean.setValue(currentString);
        }
    }


    class SearchItemViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        EditText edit_value;

        public SearchItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            edit_value = itemView.findViewById(R.id.edit_value);
        }
    }


}
